<template>
  <div id="app-container" style="margin: 10px">

    <!--搜索input框 搜索按钮 重置按钮-->
    <el-form :inline="true" style="margin-bottom: -10px">
      <el-form-item label="用户名:">
        <el-input v-model="queryOrderDTO.name" size="small" placeholder="请输入用户名"/>
      </el-form-item>

      <el-form-item label="订单交易号:">
        <el-input v-model="queryOrderDTO.orderNo" size="small" placeholder="请输入订单号"/>
      </el-form-item>

      <el-form-item label="添加时间:">
        <el-date-picker
          v-model="queryOrderDTO.createTime"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          size="small"/>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryOrderDTO.updateTime"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          size="small"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="userPageListQuery()">搜索</el-button>
        <el-button plain icon="el-icon-refresh" size="small" style="margin-left: 0" @click="resetData()" >清空</el-button>
      </el-form-item>
    </el-form>

    <el-form :inline="true">
      <el-button type="warning" plain icon="el-icon-download" size="small" style="margin-left: 0" @click="handleExport">导出数据</el-button>
    </el-form>

    <!--table表格-->
    <el-table
      ref="multipleTable"
      :data="OrderList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"/>

      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (currentPage-1)*pageSize + scope.$index +1 }}
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        prop="name"
        label="用户名"
        width="150"/>

      <el-table-column
        align="center"
        prop="title"
        label="订单标题"
        width="150"/>

      <el-table-column
        align="center"
        prop="paymentType"
        label="订单类型"
        width="150"/>

      <el-table-column
        align="center"
        prop="totalFee"
        label="订单金额(分)"
        width="150"/>

      <el-table-column
        align="center"
        prop="orderNo"
        label="支付宝交易编号"
        width="150"/>

      <el-table-column
        align="center"
        prop="orderStatus"
        label="订单状态"
        width="150"/>

      <el-table-column
        align="center"
        prop="createTime"
        label="创建时间"
        width="200"
        show-overflow-tooltip/>

      <el-table-column
        align="center"
        prop="updateTime"
        label="更新时间"
        width="200"
        show-overflow-tooltip/>

    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>
<script>
import fileDownload from 'js-file-download'
import orderInfo from '../../api/orderInfo'
import axios from 'axios'
export default {
  data() {
    return {
      queryOrderDTO: {},
      OrderList: [],
      currentPage: 1,
      pageSize: 10,
      total: 1,
      multipleSelection: ['']
    }
  },
  created() {
    this.initDate()
  },
  methods: {
    listUser() {
      orderInfo.listAll().then(result => {
        this.OrderList = result.data
        console.log(this.OrderList)
      })
    },
    userPageListQuery() {
      this.initDate()
    },
    resetData() {
      this.queryOrderDTO = {}
      this.initDate()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.initDate()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initDate()
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = []
      for (let i = 0; i < val.length; i++) {
        if (this.multipleSelection.indexOf(val[i].id) === -1) {
          this.multipleSelection.push(val[i].id)
        }
      }
      console.log(this.multipleSelection)
    },
    initDate() {
      orderInfo.listPage(this.currentPage, this.pageSize, this.queryOrderDTO).then(response => {
        this.OrderList = response.data.data
        this.total = response.data.total
      })
    },
    handleExport() {
      console.log('===导出数据===')
      axios({
        method: 'post',
        url: 'http://localhost:8080/orderInfo/export',
        responseType: 'blob',
        data: this.queryOrderDTO
      }).then(res => {
        if (res && res.data) {
          fileDownload(res.data, '订单信息.xlsx')
        }
      })
    }
  }
}
</script>
